package com.taxiApp.controller;

import com.taxiApp.domain.User;
import com.taxiApp.server.UserService;
import com.taxiApp.util.BaseResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Encoder;

import java.security.MessageDigest;
import java.util.List;

@Controller
@RequestMapping("/taxi")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 注册
     * @param user
     * @return
     */
    @RequestMapping(value = "/register",method = RequestMethod.POST )
    @ResponseBody
    public BaseResponse register(@RequestBody User user) throws Exception {

        user.setUserWork(1);
        User vo = userService.getUser(user);
        if (vo != null) {
            return new BaseResponse(500, false, "用户存在");
        }
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
        String encryPwd = base64en.encode(md5.digest(user.getPassword().getBytes("utf-8")));

        user.setPassword(encryPwd);
        userService.saveUser(user);

        return BaseResponse.SUCCESS;

    }

    /**
     * 修改密码
     * @param user
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/change",method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse change(@RequestBody User user) throws Exception {

        if (user.getEmail() == null) {
            return new BaseResponse(500, false, "邮箱不能为空");

        }

        if (user.getUserName() == null) {
            return new BaseResponse(500, false, "名称不能为空");

        }

        if(userService.getUser(user)==null){
            return new BaseResponse(500, false, "用户不为空");
        }

        MessageDigest md5=MessageDigest.getInstance("MD5");

        BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
        String encryPwd=base64en.encode(md5.digest(user.getPassword().getBytes("utf-8")));

        user.setPassword(encryPwd);

        userService.changePwd(user);

        return BaseResponse.SUCCESS;
    }

    /**
     * 登录 ---更新经纬度
     * @param user
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse login(@RequestBody User user) throws Exception {

        User vo = userService.getUser(user);
        if(vo == null){
            return new BaseResponse(500, false, "用户不存在");

        }

        userService.update(user);

        MessageDigest md5 = MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
        String loginPwd = base64en.encode(md5.digest(user.getPassword().getBytes("utf-8")));
        String oldPed = vo.getPassword(); //数据库密码

        if (loginPwd.equals(oldPed)) {
            return new BaseResponse(200,true,"登录成功",vo);
        } else {
            return new BaseResponse(500, false, "账号或密码错误");

        }

    }

    /**
     * 获取所有上班司机
     * @return
     */
    @RequestMapping(value = "/getAllDriver",method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getAllDriver() {

        List<User> allDriver = userService.getAllDriver();
        if (allDriver.size() > 0) {
            return new BaseResponse.Builder()
                    .result(true)
                    .data(allDriver)
                    .build();

        } else {
            return new BaseResponse(404, false, "当前没有上班司机");
        }

    }


    /**
     * 获取所有叫车乘客
     * @return
     */
    @RequestMapping(value = "/getAllPassenger",method = RequestMethod.GET)
    @ResponseBody
    public BaseResponse getAllPassenger() {

        List<User> allPassenger = userService.getAllPassenger();
        if (allPassenger.size() > 0) {
            return new BaseResponse.Builder()
                    .result(true)
                    .data(allPassenger)
                    .build();
        } else {
            return new BaseResponse(404, false, "当前没有叫车乘客");
        }

    }




    @RequestMapping(value = "/updateLocation",method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse updateLocation(@RequestBody User user){

        try {
            userService.update(user);

            return BaseResponse.SUCCESS;
        }catch (Exception ex){
            return new BaseResponse(500, false, ex.getMessage());
        }
    }


    /**
     * 开始等待上车
     * @param user
     * @return
     */
    @RequestMapping(value = "/waiting",method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse waiting(@RequestBody User user){

        try {

            if (user.getUserWork() == 1) {//如果是空闲 则等待
                user.setUserWork(2);
                userService.update(user);
                return BaseResponse.SUCCESS;

            }else {
                return new BaseResponse(500, false, "当前用户不是空闲不能开始等待！");
            }

        }catch (Exception ex){
            return new BaseResponse(500, false, ex.getMessage());
        }
    }


    /**
     *  上车中
     * @param user
     * @return
     */
    @RequestMapping(value = "/working",method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse working(@RequestBody User user){

        try {

            if (user.getUserWork() == 2) {//如果是等待 则工作
                user.setUserWork(3);
                userService.update(user);
                return BaseResponse.SUCCESS;

            }else {
                return new BaseResponse(500, false, "当前用户不是等待不能开始工作！");
            }

        }catch (Exception ex){
            return new BaseResponse(500, false, ex.getMessage());
        }
    }


    /**
     * 完成行程
     * @param user
     * @return
     */
    @RequestMapping(value = "/finish",method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse finish(@RequestBody User user){


        user.setUserWork(1);

        userService.update(user);

        return BaseResponse.SUCCESS;

    }


}
